package algotrader.book;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class Book implements Serializable {

	private static final long serialVersionUID = 1L;
	private String ativo;
	private List<BookLine> compraLines;
	private List<BookLine> vendaLines;

	
	public Book(String ativo) {
		this.ativo = ativo;
		this.compraLines = new ArrayList<BookLine>();
		this.vendaLines = new ArrayList<BookLine>();
	}
	
	public String getAtivo() {
		return ativo;
	}
	
	public List<BookLine> getVendaBookLines() {
		return vendaLines;
//		List<BookLine> aux = new LinkedList<BookLine>();
//		Iterator<BookLine> i = vendaLines.iterator();
//		while(i.hasNext()) aux.add(i.next());
//		//ordenanda lista
//		Collections.sort(aux,new BookLineComparator());
//		return aux;
	}
	
	public List<BookLine> getCompraBookLines() {
		return compraLines;
//		List<BookLine> aux = new LinkedList<BookLine>();
//		Iterator<BookLine> i = compraLines.iterator();
//		while(i.hasNext()) aux.add(i.next());
//		//ordenanda lista
//		Collections.sort(aux,new BookLineComparator());
//		Collections.reverse(aux);
//		return aux;
	}
	
	public int hashCode() {
		return ativo.hashCode();
	}
	
	public boolean equals(Object o) {
		Book b = (Book)o;
		return this.ativo.equals(b.getAtivo());
	}

	public void addBookLine(BookLine bookLine) {
		if(bookLine.getTipo()==BookLineType.COMPRA)
			compraLines.add(bookLine.getPosicao(),bookLine);
		else
			vendaLines.add(bookLine.getPosicao(),bookLine);
	}
	
	public void atualizaBookline(int oldPosition, BookLine bookLine) {
		if(bookLine.getTipo()==BookLineType.COMPRA) {
			compraLines.remove(oldPosition);
			compraLines.add(bookLine.getPosicao(), bookLine);
		} else {
			vendaLines.remove(oldPosition);
			vendaLines.add(bookLine.getPosicao(), bookLine);
		}
	}
	
	public void addBookLines(List<BookLine> bls) {
		if(bls.get(0).getTipo() == BookLineType.COMPRA)
			for(BookLine bl : bls)
				compraLines.add(bl.getPosicao(),bl);
		else
			for(BookLine bl : bls)
				vendaLines.add(bl.getPosicao(),bl);
	}

	public void removeBookLine(BookLine bookLine) {
		if(bookLine.getTipo()==BookLineType.COMPRA)
			compraLines.remove(bookLine.getPosicao());
		else
			vendaLines.remove(bookLine.getPosicao());
	}

	public void removeAll() {
		compraLines.clear();
		vendaLines.clear();
	}

	public void removeAllBetter(BookLineType tipo, int max) {
		if(tipo == BookLineType.COMPRA)
			for(int i = 0 ; i < max; i++) compraLines.remove(i);
		else
			for(int i = 0 ; i < max; i++) vendaLines.remove(i);
			
	}

	public void remove(BookLineType tipo, int posicao) {
		if(tipo == BookLineType.COMPRA)
			compraLines.remove(posicao);
		else
			vendaLines.remove(posicao);
	}
	
	public String toString() {
		String result = "";
		for (BookLine bl : compraLines)
			result += bl.getAtivo() +" - "+bl.getPosicao()+" - "+bl.getPreco();
		return result;
	}
}
